<template>
    <el-container style="height: 500px; border: 1px solid #eee">
        <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
            <el-menu>
                <el-submenu :index="''+permission.id" v-for="permission in permissions">
                    <template slot="title">
                        <router-link :to="permission.url">
                            <i :class="permission.icon"></i>
                            {{permission.name}}
                        </router-link>
                    </template>
                    <el-menu-item :index="''+child.id" v-for="child in permission.childPermissions"
                                  v-show="child.url != null">
                        <router-link :to="child.url">
                            <i :class="child.icon"></i>
                            {{child.name}}
                        </router-link>
                    </el-menu-item>
                </el-submenu>
            </el-menu>
        </el-aside>

        <el-container>
            <el-header style="text-align: right; font-size: 12px">
                <el-dropdown>
                    <i class="el-icon-setting" style="margin-right: 15px"></i>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>查看</el-dropdown-item>
                        <el-dropdown-item>新增</el-dropdown-item>
                        <el-dropdown-item>删除</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <span>王小虎</span>
            </el-header>

            <el-main>
                <!--挂载子页面-->
                <router-view/>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    import router from "@/router/index.js"

    export default {
        data() {
            return {
                permissions: [], //权限集合
                routes: []
            }
        },
        methods: {
            //读取菜单权限
            loadPermissions() {
                let username = localStorage.getItem("username");
                this.$http.get("/user-permissions/" + username)
                    .then(result => {
                        console.log(result.data)
                        if (result.data.code == 200) {
                            this.permissions = result.data.data;
                            this.addPermissionRoutes()
                        }
                    })
            },
            addPermissionRoutes() {
                let main =
                {
                    path: '/main',
                    name: 'main',
                    component: () => import('@/views/MainView'),
                    children:[
                    ]
                }
                this.permissions.forEach(permission => {
                    if(permission.url == null || permission.description == null || permission.description == ''){
                        permission.url = '/404'
                        permission.description = 'NotFound'
                    }
                    let child = {
                        path: permission.url,
                        name: permission.description,
                        component: () => import('@/views/' + permission.description)
                    }
                    main.children.push(child)
                    permission.childPermissions.forEach(cp => {
                        if(cp.url == null || cp.description == null || cp.description == ''){
                            cp.url = '/404'
                            cp.description = 'NotFound'
                        }
                        let c = {
                            path: cp.url,
                            name: cp.description,
                            component: () => import('@/views/' + cp.description)
                        }
                        main.children.push(c)
                    })
                })
                router.addRoute(main)
            }
        },
        mounted() {
            this.loadPermissions()
        }
    };
</script>

<style scoped>
    .el-header {
        background-color: #B3C0D1;
        color: #333;
        line-height: 60px;
    }

    .el-aside {
        color: #333;
    }
</style>
